/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package tRM_RTNB.impl;

import node1.impl.Node1Impl;

import org.eclipse.emf.ecore.EClass;

import resultsWCRT.ResultsWCRT;
import resultsWCRT.ResultsWCRTFactory;
import resultsWCRT.Scheduling;
import tRM_RTNB.TRM_RTNB;
import tRM_RTNB.TRM_RTNBPackage;
import utils.UtilsMaths;
import utils.UtilsModelInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>TRM RTNB</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class TRM_RTNBImpl extends Node1Impl implements TRM_RTNB {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TRM_RTNBImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return TRM_RTNBPackage.Literals.TRM_RTNB;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public void compute() {

		/**/
		if (todo){
			
			/**/
			ResultsWCRT rWCRT = UtilsModelInstance.createResultsWCRT();
			UtilsModelInstance.linkComputeBox(this, rWCRT, false);
			
			/* Order list by priority */
			UtilsModelInstance.orderByPriority(this.tasks);

						
			/**/
			for (int i = 0 ; i < this.tasks.size() ; i++) {

				System.out.println(i);
				int a_0 = this.tasks.get(i).getWcet();
				int a_n = 0;
				/**/
				for (int j = 0 ; j < i ; j++) {

					a_0 = a_0 + this.tasks.get(j).getWcet();

				} // Loop j

				/**/
				while (a_n != a_0) {

					/**/
					a_0 = a_n;
					a_n = this.tasks.get(i).getWcet();

					/**/
					for (int j = 0 ; j < i ; j++) {

						a_n = a_n + UtilsMaths.ceiling(a_0, this.tasks.get(j).getPeriod()) * this.tasks.get(j).getWcet();

					} // Loop j

				}

				/**/
				resultsWCRT.LocalTask lt = ResultsWCRTFactory.eINSTANCE.createLocalTask();
				lt.setId(this.tasks.get(i).getId());
				lt.setWcrt(a_n);
				
				/**/
				System.out.println(">> " + this.tasks.get(i).getDeadline());
				if (a_n <= this.tasks.get(i).getDeadline()) {

					lt.setScheduling(Scheduling.SCHEDULABLE);

				}

				/**/
				else {

					lt.setScheduling(Scheduling.NON_SCHEDULABLE);
				}
				
				/**/
				rWCRT.getTasks().add(lt);
			} // Loop i

			
			rWCRT.compute();
			this.todo = false;

		}

} //TRM_RTNBImpl
